//
//  PrettyViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "PrettyViewController.h"
#import "JokeTableViewCell.h"
#import "JokeModel.h"
#import "PicViewController.h"
#import "MJRefresh.h"
#import "VideoViewController.h"
@interface PrettyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,assign) NSInteger temp;//点击标识
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UITableView *picTableView;
@property (nonatomic,strong) NSMutableArray *jokeArray;//装段子的数组
@property (nonatomic,strong) PicViewController *picVC;//趣图
@property (nonatomic,strong) VideoViewController *videoVC;//视频
@property (nonatomic,strong) UIView *jokeView;//段子
@property (nonatomic,strong) UIView *view2;//趣图
@property (nonatomic,strong) UIView *view3;//视频
@property (nonatomic,assign) NSInteger page;//页码
@property (nonatomic,strong) UISegmentedControl *segment;
@end

@implementation PrettyViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    // 调用解析段子的方法
    [self jiexiJokeURL:kJokerUrl];
    
    
   
    //创建UISegment
    NSArray *titleArray = @[@"段子",@"趣图",@"视频"];
    self.segment = [[UISegmentedControl alloc]initWithItems:titleArray];
    self.segment.frame =CGRectMake(75, 10, 225, 30);
    //是否留下填充颜色,默认是NO(留下)
    //segment.momentary = YES;
    self.segment.tintColor = [UIColor colorWithRed:0.423 green:0.452 blue:0.591 alpha:1.000];
    self.segment.selectedSegmentIndex = 0;
    //[segment setTitle:@"逗你玩" forSegmentAtIndex:1];
    [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _segment;

    // 创建tableView,并添加到view1上
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.allowsSelection = NO;
    //self.tableView.backgroundColor = [UIColor redColor];
    [self.tableView registerClass:[JokeTableViewCell class] forCellReuseIdentifier:@"jokeClass"];
    
    //设置毛玻璃效果
    GroundGlass *backImage = [[GroundGlass alloc]initWithFrame:[UIScreen mainScreen].bounds style:UIBlurEffectStyleLight];
    self.tableView.backgroundView = backImage;
    
    
    self.jokeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    self.jokeView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_jokeView];
    [self.jokeView addSubview:_tableView];
    self.jokeView.backgroundColor = [UIColor colorWithPatternImage:backImage.image];
    
    //下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    
    //上拉加载
    //self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.tableView.footer = footer;

    // 设置文字
    [footer setTitle:@"点击加载" forState:MJRefreshStateIdle];
    [footer setTitle:@"加载更多 ..." forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据..." forState:MJRefreshStateNoMoreData];
    // 设置字体
    footer.stateLabel.font = [UIFont systemFontOfSize:15];
    // 设置颜色
    footer.stateLabel.textColor = [UIColor blueColor];
    
    // 给page一个初始值
    _page = 2;

    [self.view bringSubviewToFront:_jokeView];
}


- (NSMutableArray *)jokeArray
{
    if (!_jokeArray) {
        _jokeArray = [[NSMutableArray alloc]init];
    }
    return _jokeArray;
}

//下拉刷新
- (void)headerRefresh
{
    [self jiexiJokeURL:kJokerUrl];
    [self.tableView.header endRefreshing];
}
- (void)footerRefresh
{
    [self jiexiNextJokeUrl];
    [self.tableView.footer endRefreshing];
}

- (void)jiexiNextJokeUrl
{
    NSString *str = [NSString stringWithFormat:@"%ld",_page];
    NSString *url = [[kBeforeJokeUrl stringByAppendingString:str]stringByAppendingString:kAfterJokeUrl];
    [self jiexiJokeURL:url];
    _page ++;
    NSLog(@"page == %ld",_page);
}

- (void)jiexiJokeURL:(NSString *)url
{
    [LORequestManger GET:url success:^(id response) {
        NSArray *array = [response objectForKey:@"items"];
        
//        self.jokeArray = [NSMutableArray array];
        
        NSMutableArray *arr = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            JokeModel *model = [[JokeModel alloc]init];
            model.content = [dic objectForKey:@"content"];
            [arr addObject:model];
        }
        [self.jokeArray addObjectsFromArray:arr];
        
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog( @"%@",error);
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.jokeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    static NSString *indentifier = @"jokeClass";
//    [tableView registerClass:[JokeTableViewCell class] forCellReuseIdentifier:indentifier];
    JokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jokeClass" forIndexPath:indexPath];
    
    JokeModel *model = _jokeArray[indexPath.row];
    
    cell.backgroundColor = [UIColor clearColor];
    [cell cellSetModel:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JokeModel *model = [self.jokeArray objectAtIndex:indexPath.row];
    return [JokeTableViewCell heightForCell:model];
}

//点击segment触发的方法
- (void)segmentAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {

        [self.view bringSubviewToFront:_jokeView];
    }
    else if (segment.selectedSegmentIndex == 1)
    {
        // 添加趣图的子控制器
        self.picVC = [[PicViewController alloc]init];
        [self addChildViewController:_picVC];
        self.picVC.view.frame = CGRectMake(0, 0, kWidth, kHeight);
        [self.view addSubview:_picVC.view];

        [self.view bringSubviewToFront:_picVC.view];
    }
    else
    {
        // 添加视频的自控制器
        self.videoVC = [[VideoViewController alloc]init];
        [self addChildViewController:_videoVC];
        self.videoVC.view.frame = CGRectMake(0, 0, kWidth, kHeight);
        [self.view addSubview:_videoVC.view];

        [self.view bringSubviewToFront:_videoVC.view];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
